package com.wgl.express.service.Impl;

import com.wgl.express.entity.User;
import com.wgl.express.mapper.UserMapper;
import com.wgl.express.service.UserService;
import com.wgl.express.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

public class UserServiceImpl implements UserService {

    public static User currentUser;

    @Override
    public int login(String username, String password) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            User user ;
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            if ((user =userMapper.selectUserByName(username) )!= null){
                if (user.verifyPassword(password)){
                    currentUser = user;
                    return user.getType();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int register(User user) {
        int s = 0;
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            s=userMapper.insertUser(user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }

    @Override
    public int changeinfo(User user) {
        return 0;
    }

    @Override
    public List<User> getPostOfStation(int sid) {
        List<User> users = null;
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            users = userMapper.selectPostBySid(sid);
        }catch (Exception e){
            String message = e.getMessage();
            System.out.println(message);
        }
        return users;
    }

    @Override
    public int insertPackToPost(int uid, int pid) {
        int s = 0;
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            s = userMapper.insertPackToPost(uid,pid);
        }catch (Exception e){
            String message = e.getMessage();
            System.out.println(message);
        }
        return s;
    }

    @Override
    public int setPassword(User user) {
        int s = 0;
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            s = userMapper.updateUser(user);
        }catch (Exception e){
            String message = e.getMessage();
            System.out.println(message);
        }
        return s;
    }

    @Override
    public int setPhone(User u) {
        int s = 0;
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            s = userMapper.updateUser(u);
        }catch (Exception e){
            String message = e.getMessage();
            System.out.println(message);
        }
        return s;
    }
}
